package org.ajc.auth.controller;

import org.ajc.auth.service.IPermissionService;
import org.ajc.auth.domain.Permission;
import org.ajc.auth.query.PermissionQuery;
import org.ajc.base.annotation.AjcPermission;
import org.ajc.base.util.AjaxResult;
import org.ajc.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("/permission")
@AjcPermission(name = "权限管理", desc = "权限相关接口")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;


    /**
     * 保存和修改公用的
     * @param permission  传递的实体
     * @return Ajaxresult 转换结果
     */
    @PutMapping
    @AjcPermission(name = "权限保存", desc = "权限的修改和新增接口")
    public AjaxResult save(@RequestBody Permission permission){
        try {
            if( permission.getId()!=null)
                permissionService.update(permission);
            else
                permissionService.insert(permission);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @AjcPermission(name = "权限删除", desc = "根据id删除权限接口")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            permissionService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error("删除对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除多个信息
    */

    @DeleteMapping
    @AjcPermission(name = "权限删除", desc = "批量删除权限接口")
    public AjaxResult batchDelete(List<Serializable> ids){
        try {
                permissionService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除对象失败！"+e.getMessage());
        }
    }
    //获取用户
    @AjcPermission(name = "权限查询", desc = "根据id查询权限接口")
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id")Long id)
    {
        try {
            Permission permission = permissionService.selectById(id);
            return AjaxResult.success(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！"+e.getMessage());
        }
    }
    /**
    * 查看所有的权限信息
    * @return
    */

    @AjcPermission(name = "权限查询", desc = "查询所有权限接口")
    public AjaxResult selectAll(){

        try {
            List< Permission> list = permissionService.selectAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！"+e.getMessage());
        }
    }
    /**
    * 分页查询数据
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @AjcPermission(name = "权限分页",desc = "权限的分页查询")
    public AjaxResult queryPage(@RequestBody PermissionQuery query)
    {
        try {
            PageInfo<Permission> pageInfo = permissionService.queryPage(query);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！"+e.getMessage());
        }
    }

    //获取权限树
    @GetMapping("/deptTree")
    public AjaxResult getTree()
    {
        try {
            List<Permission> permissionList = permissionService.getTree();
            return AjaxResult.success(permissionList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取权限树失败！"+e.getMessage());
        }
    }
}
